<?php
/*
 * @author Khanh.Vu
 * 
 */
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }

Class StaticController Extends baseController {
	private $numOfRow = 1;
	private $page = 1;

	function __construct($registry) {
		$this->registry = $registry;
		$this->numOfRow = $this->registry->db->get('configManager')->getByKey('StaticTableSize')->get('value');
	}

	public function index($user)
	{
		$this->view($user, $this->page);
	}

	public function view($currentUser, $page)
	{
		$content = <<<EOT
Welcome to Book Control Panel!
EOT;

		if(isset($_GET['numRows'])){
			$this->numOfRow = $_GET['numRows'];
		}else if(isset($_GET['curRow'])){
			$this->numOfRow = $_GET['curRow'];
		}

		$user = "Admin";
		//$path = BSTO_ROOT_PATH."static";


		if ($page == null){
			$this->page = 1;
		} else {
			$this->page = $page;
		}
		if ($user == "Normal") {
			$content = "You dont have permission for this page!";
		}
		else {
			$content = $this->registry->template->getStaticTableContent($this->page, $this->numOfRow);
		}

		$tags = array('bstoContentTitle' => 'Static Information');
		$this->registry->template->setMenuKey('static');
		$output = $this->registry->template->getPageContent('index', 'BSTO ACP - Static Management', $content, $tags);
		echo $output;
	}

/*	public function add($user)
	{
		//$row = $_GET['numRows'];
		if(isset($_GET['numRows'])){
			$this->numOfRow = $_GET['numRows'];
		}else if(isset($_GET['curRow'])){
			$this->numOfRow = $_GET['curRow'];
		}

		$method = $_SERVER['REQUEST_METHOD'];
		$tags = array('bstoContentTitle' => 'Add A New Static');
		$uid = 2;
		if ($method=='POST'){
			//$user = $this->registry->db->get('userManager')->getCurrentUser();

			$type = "static";
			$status = 1;
			$title = strip_tags($_POST["title"]);

			$created = $this->registry->db->get('staticManager')->getNow();
			$detail = strip_tags($_POST["detail"]);

			$queryEntity = "INSERT INTO entity(title, type, uid, created, status)
							VALUES (" . var_export($title, true) . "," .var_export($type, true). "," . $uid . ", NOW(),". $status. ")";

			$this->registry->db->dbQuery($queryEntity);
			$nid = $this->registry->db->get('staticManager')->getMaxId();
			$queryStatic = "INSERT INTO static (nid, detail, value) VALUES ($nid," . var_export($detail, true) .",". var_export($title, true) .")";
			$this->registry->db->dbQuery($queryStatic);

			//come to last page
			$totalStatic = $this->registry->db->get('staticManager')->countStatic();
			$page = ceil($totalStatic/$this->numOfRow);
			header("Location: ../view/$page");
			exit();
		} else {
			$content = $this->registry->template->getModule('astatic', $tags);
		}

		$output = $this->registry->template->getPageContent('index', 'book', $content, $tags);
			
		echo $output;
			
	}*/

	public function edit($user, $page, $id)
	{
		//$row = $_GET['numRows'];
		if(isset($_GET['numRows'])){
			$this->numOfRow = $_GET['numRows'];
		}else if(isset($_GET['curRow'])){
			$this->numOfRow = $_GET['curRow'];
		}

		$method = $_SERVER['REQUEST_METHOD'];
		$tags = array('bstoContentTitle' => 'Edit A Static');
		if ($method=='POST'){
			$type = "static";

			//$uid = $user->uid;
			$uid = 1;

			$created = $this->registry->db->get('staticManager')->getNow();
			//$title = strip_tags($_POST["title"]);
			$detail = strip_tags($_POST["detail"]);

			$status = 1;

			$queryEntity = "UPDATE entity
							SET  uid = $uid, changed = NOW()
							WHERE nid = $id";

			$queryStatic = "UPDATE static
					      SET  detail = " . var_export($detail, true)  . 
					     "WHERE nid = $id";

			$this->registry->db->dbQuery($queryEntity);
			$this->registry->db->dbQuery($queryStatic);

			//back to view page
			$totalStatic = $this->registry->db->get('staticManager')->countStatic();
			$page = ($page <= ceil($totalStatic/$this->numOfRow)) ? $page : $totalStatic/$this->numOfRow;
			header("Location: ../../view/$page");
			exit();
		}else{
			//echo "id = ".$id;
			$arr = $this->registry->db->get('staticManager')->getStaticById($id);
			//echo count($arr);
			$obj = $arr[0];

			$tmpContent = $this->registry->template->getModule('estatic', $tags);

			//$arrResult = $this->registry->db->get('catManager')->getBookByUser($id, $type);
			$arrResult = $this->registry->db->get('bookManager')->getBookByUser(2, "admin");
			$arr2 = $this->registry->db->get('bookManager')->getRelatedBookName($id);

			$content = str_replace('{title}', $obj->get('title'), $tmpContent);
			$content = str_replace('{detail}', $obj->get('detail'), $content);

			$this->registry->template->setMenuKey('static');
			$output = $this->registry->template->getPageContent('index', 'BSTO ACP - Edit Static', $content, $tags);

			echo $output;
		}
	}

	/*public function delete($user, $page, $id)
	{
		$staMan = $this->registry->db->get('staticManager');
		
		//not delete default page
		$obj = $staMan->getStaticById($id);
		if ($obj[0]->get('value') != 'Default Page') {
			$staMan->setStatusById($id, 0);	
						
		}
		
		$totalStatic = $staMan->countStatic();
		$page = ($page <= ceil($totalStatic/$this->numOfRow)) ? $page : $totalStatic/$this->numOfRow;
		header("Location: ../../view/$page");
		exit();
	}*/

	/*public function multiEdit() {
		echo count($_POST['idArray']);
	}*/
}
?>
